﻿using System;
using System.Configuration;
using JY.IOC.Configuration.Elements;

namespace JY.IOC.Configuration
{
    /// <summary>
    /// Ioc配置节点
    /// </summary>
    public class IocSection : ConfigurationSection
    {
        #region # 常量

        private const string ASSEMBLY_STR = "assembly";
        private const string TYPE_STR = "type";

        private const string ONLY_REGISTER_INTERFACES_ASSEMBLIES_STR = "onlyRegisterInterfacesAssemblies";
        private const string ONLY_REGISTER_IMPLEMENTS_ASSEMBLIES_STR = "onlyRegisterImplementsAssemblies";
        private const string ALL_REGISTER_ASSEMBLIES_STR = "allRegisterAssemblies";
        private const string ALL_REGISTER_BASEASSEMBLIES_STR = "allBaseRegisterAssemblies";
        private const string WCF_REGISTER_INTERFACES_ASSEMBLIES_STR = "wcfRegisterInterfacesAssemblies";

        private const string REGISTER_INTERFACE_TYPES_STR = "registerInterfaceTypes";
        private const string REGISTER_TYPES_STR = "registerTypes";
        private const string REGISTER_BASETYPES_STR = "registerBaseTypes";
        #endregion

        #region # 字段

        /// <summary>
        /// Ioc配置节点
        /// </summary>
        private static readonly IocSection _IocSection;
        #endregion

        #region # 构造器

        /// <summary>
        /// 静态构造器
        /// </summary>
        static IocSection()
        {
            _IocSection = (IocSection)ConfigurationManager.GetSection("jy.ioc");

            #region # 非空验证

            if (_IocSection == null)
            {
                throw new ApplicationException("jy.ioc节点未配置，请检查配置文件！");
            }

            #endregion
        }
        #endregion

        #region # 访问器 —— static IocSection Section
        /// <summary>
        /// 访问器
        /// </summary>
        public static IocSection Section
        {
            get { return _IocSection; }
        }
        #endregion

        #region # 仅注册接口的程序集列表 —— AssemblyElementCollection OnlyRegisterInterfacesAssemblies
        /// <summary>
        /// 仅注册接口的程序集列表
        /// </summary>
        [ConfigurationProperty(ONLY_REGISTER_INTERFACES_ASSEMBLIES_STR)]
        [ConfigurationCollection(typeof(AssemblyElementCollection), AddItemName = ASSEMBLY_STR)]
        public AssemblyElementCollection OnlyRegisterInterfacesAssemblies
        {
            get
            {
                AssemblyElementCollection collection = this[ONLY_REGISTER_INTERFACES_ASSEMBLIES_STR] as AssemblyElementCollection;
                return collection ?? new AssemblyElementCollection();
            }
            set { this[ONLY_REGISTER_INTERFACES_ASSEMBLIES_STR] = value; }
        }
        #endregion

        #region # 仅注册实现的程序集列表 —— AssemblyElementCollection OnlyRegisterImplementsAssemblies
        /// <summary>
        /// 仅注册实现的程序集列表
        /// </summary>
        [ConfigurationProperty(ONLY_REGISTER_IMPLEMENTS_ASSEMBLIES_STR)]
        [ConfigurationCollection(typeof(AssemblyElementCollection), AddItemName = ASSEMBLY_STR)]
        public AssemblyElementCollection OnlyRegisterImplementsAssemblies
        {
            get
            {
                AssemblyElementCollection collection = this[ONLY_REGISTER_IMPLEMENTS_ASSEMBLIES_STR] as AssemblyElementCollection;
                return collection ?? new AssemblyElementCollection();
            }
            set { this[ONLY_REGISTER_IMPLEMENTS_ASSEMBLIES_STR] = value; }
        }
        #endregion

        #region # 注册程序集列表 —— AssemblyElementCollection AllRegisterAssemblies
        /// <summary>
        /// 注册程序集列表
        /// </summary>
        [ConfigurationProperty(ALL_REGISTER_ASSEMBLIES_STR)]
        [ConfigurationCollection(typeof(AssemblyElementCollection), AddItemName = ASSEMBLY_STR)]
        public AssemblyElementCollection AllRegisterAssemblies
        {
            get
            {
                AssemblyElementCollection collection = this[ALL_REGISTER_ASSEMBLIES_STR] as AssemblyElementCollection;
                return collection ?? new AssemblyElementCollection();
            }
            set { this[ALL_REGISTER_ASSEMBLIES_STR] = value; }
        }
        #endregion

        #region # 注册基类程序集列表 —— AssemblyElementCollection AllRegisterBaseAssemblies
        /// <summary>
        /// 注册基类程序集列表
        /// </summary>
        [ConfigurationProperty(ALL_REGISTER_BASEASSEMBLIES_STR)]
        [ConfigurationCollection(typeof(AssemblyElementCollection), AddItemName = ASSEMBLY_STR)]
        public AssemblyElementCollection AllRegisterBaseAssemblies
        {
            get
            {
                AssemblyElementCollection collection = this[ALL_REGISTER_BASEASSEMBLIES_STR] as AssemblyElementCollection;
                return collection ?? new AssemblyElementCollection();
            }
            set { this[ALL_REGISTER_BASEASSEMBLIES_STR] = value; }
        }
        #endregion

        #region # 注册WCF接口程序集列表 —— AssemblyElementCollection WcfRegisterInterfacesAssemblies
        /// <summary>
        /// 注册WCF接口程序集列表
        /// </summary>
        [ConfigurationProperty(WCF_REGISTER_INTERFACES_ASSEMBLIES_STR)]
        [ConfigurationCollection(typeof(AssemblyElementCollection), AddItemName = ASSEMBLY_STR)]
        public AssemblyElementCollection WcfRegisterInterfacesAssemblies
        {
            get
            {
                AssemblyElementCollection collection = this[WCF_REGISTER_INTERFACES_ASSEMBLIES_STR] as AssemblyElementCollection;
                return collection ?? new AssemblyElementCollection();
            }
            set { this[WCF_REGISTER_INTERFACES_ASSEMBLIES_STR] = value; }
        }
        #endregion

        #region # 注册接口类型列表 —— TypeElementCollection RegisterInterfaceTypes
        /// <summary>
        /// 注册接口类型列表
        /// </summary>
        [ConfigurationProperty(REGISTER_INTERFACE_TYPES_STR)]
        [ConfigurationCollection(typeof(TypeElementCollection), AddItemName = TYPE_STR)]
        public TypeElementCollection RegisterInterfaceTypes
        {
            get
            {
                TypeElementCollection collection = this[REGISTER_INTERFACE_TYPES_STR] as TypeElementCollection;
                return collection ?? new TypeElementCollection();
            }
            set { this[REGISTER_INTERFACE_TYPES_STR] = value; }
        }
        #endregion

        #region # 注册类类型列表 —— TypeElementCollection RegisterTypes
        /// <summary>
        /// 注册类类型列表
        /// </summary>
        [ConfigurationProperty(REGISTER_TYPES_STR)]
        [ConfigurationCollection(typeof(TypeElementCollection), AddItemName = TYPE_STR)]
        public TypeElementCollection RegisterTypes
        {
            get
            {
                TypeElementCollection collection = this[REGISTER_TYPES_STR] as TypeElementCollection;
                return collection ?? new TypeElementCollection();
            }
            set { this[REGISTER_TYPES_STR] = value; }
        }
        #endregion

        #region # 注册基类类型列表 —— TypeElementCollection RegisterBaseTypes
        /// <summary>
        /// 注册基类类型列表
        /// </summary>
        [ConfigurationProperty(REGISTER_BASETYPES_STR)]
        [ConfigurationCollection(typeof(TypeElementCollection), AddItemName = TYPE_STR)]
        public TypeElementCollection RegisterBaseTypes
        {
            get
            {
                TypeElementCollection collection = this[REGISTER_BASETYPES_STR] as TypeElementCollection;
                return collection ?? new TypeElementCollection();
            }
            set { this[REGISTER_BASETYPES_STR] = value; }
        }
        #endregion
    }
}
